package com.ruoyi.livedemo.mapper;

import com.ruoyi.livedemo.entity.AlbumMedia;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AlbumMediaMapper {

    @Select("SELECT * FROM album_media WHERE user_id=#{userId} ORDER BY taken_at DESC, id DESC")
    List<AlbumMedia> findByUser(Long userId);

    @Insert("INSERT INTO album_media (user_id, title, description, media_type, file_path, taken_at, mood) " +
            "VALUES (#{userId}, #{title}, #{description}, #{mediaType}, #{filePath}, #{takenAt}, #{mood})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(AlbumMedia media);

    @Delete("DELETE FROM album_media WHERE id=#{id} AND user_id=#{userId}")
    int delete(@Param("id") Long id, @Param("userId") Long userId);

    @Select("SELECT * FROM album_media WHERE user_id=#{userId} ORDER BY taken_at DESC, id DESC LIMIT #{limit} OFFSET #{offset}")
    List<AlbumMedia> findPage(@Param("userId") Long userId,
                              @Param("offset") int offset,
                              @Param("limit") int limit);

    @Select("SELECT COUNT(1) FROM album_media WHERE user_id=#{userId}")
    long countByUser(Long userId);

    @Select("SELECT * FROM album_media WHERE id=#{id} AND user_id=#{userId}")
    AlbumMedia findByIdAndUser(@Param("id") Long id, @Param("userId") Long userId);

    @Update("UPDATE album_media SET title=#{title}, description=#{description}, media_type=#{mediaType}, file_path=#{filePath}, " +
            "taken_at=#{takenAt}, mood=#{mood} WHERE id=#{id} AND user_id=#{userId}")
    int update(AlbumMedia media);
}
